// pages/media/media.js
const backgroundAudioManager = wx.getBackgroundAudioManager()

backgroundAudioManager.title = '此时此刻'
backgroundAudioManager.epname = '此时此刻'
backgroundAudioManager.singer = '汪峰'
backgroundAudioManager.startTime = 10
backgroundAudioManager.coverImgUrl = 'http://y.gtimg.cn/music/photo_new/T002R300x300M000003rsKF44GyaSk.jpg?max_age=2592000'

function getRandomColor() {
  let rgb = []
  for (let i = 0; i < 3; ++i) {
    let color = Math.floor(Math.random() * 256).toString(16)
    color = color.length == 1 ? '0' + color : color
    rgb.push(color)
  }
  return '#' + rgb.join('')
}

Page({

  /**
   * 页面的初始数据
   */
  data: {
    temPaths:[],
    vidioSrc:'',
    Danmu:''
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    backgroundAudioManager.pause (function(e){
      console.log('暂停播放')
    })
    backgroundAudioManager.onNext(function(e){
      console.log('下一曲')
    })


  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
    this.audioCtx = wx.createAudioContext('myAudio')
    this.audioCtx.setSrc('http://ws.stream.qqmusic.qq.com/M500001VfvsJ21xFqb.mp3?guid=ffffffff82def4af4b12b3cd9337d5e7&uin=346897220&vkey=6292F51E1E384E061FF02C31F716658E5C81F5594D561F2E88B854E81CAAB7806D5E4F103E55D33C16F3FAC506D1AB172DE8600B37E43FAD&fromtag=46')


    this.videoCtx = wx.createVideoContext('myVideo')
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
  
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {
  
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
  
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
  
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
  
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
  
  },
  // 选择图片
  chooseImage:function(){
    var that = this
    wx.chooseImage({
      success: function(res) {
        var arr = that.data.temPaths.concat(res.tempFilePaths)
        arr.concat(res.tempFilePaths)
        console.log(arr)
        that.setData({
          temPaths: arr
        })
        console.log(that.data.temPaths)
        console.log(res.tempFilePaths)
        console.log(res.tempFiles)
      },
      fail:function(res){
        console.log(res)
      },
      sourceType:"camera",
    })
  },
  // 预览图片
  previewImage:function(){
    
    wx.previewImage({
      urls: this.data.temPaths,
      success:function(res){
        console.log("图片预览成功"+res)
      },
      fail:function(res){
        console.log("图片预览失败" + res)
      }
    })
  },
  // 获取首张图片信息
  getImgInfo:function(){
    wx.getImageInfo({
      src: this.data.temPaths[0],
      success:function(e){
        console.log(e)
      },
      fail:function(e){
        console.log(e)
      }
    })
  },
  // 保存图片到本地相册
  saveImg:function(){
    wx.saveImageToPhotosAlbum({
      filePath: '/img/game_normal.png',
      success(res){
        console.log("保存成功")
      }
    })
  },
  // 录音
  startRecord:function(){
    wx.startRecord({
      success:function(e){
        wx.playVoice({
          filePath: e.tempFilePath,
          success:function(){
            console.log("开始播放录音")
          }
        })
        console.log(e.tempFilePath)
      },
      fail:function(e){
        console.log("录音失败")
      }
    })
    setTimeout(function(){
      wx.stopRecord()
      console.log("录音结束了")
    },10000)
  },
  // 暂停播放的录音
  pasueVoice:function(){
    wx.pauseVoice()
  },
  //结束播放的录音
  stopVoice:function(){
    wx.stopVoice()
  },
  // 开启后台播放
  playBackgroundAudio:function(){
    wx.playBackgroundAudio({
      dataUrl: 'http://ws.stream.qqmusic.qq.com/M500001VfvsJ21xFqb.mp3?guid=ffffffff82def4af4b12b3cd9337d5e7&uin=346897220&vkey=6292F51E1E384E06DCBDC9AB7C49FD713D632D313AC4858BACB8DDD29067D3C601481D36E62053BF8DFEAF74C0A5CCFADD6471160CAF3E6A&fromtag=46',
      title:'此时此刻',
      coverImgUrl:'http://y.gtimg.cn/music/photo_new/T002R300x300M000003rsKF44GyaSk.jpg?max_age=2592000',
      success:function(e){
        console.log('开始播放',e)
      },
      fail:function(e){
        console.log('播放失败',e)
      }
    })
  },
  // 获取当前播放状态
  getPlayBackgrounState:function(){
    wx.getBackgroundAudioPlayerState({
      success: function (e) {
        console.log("播放状态", e)
      }
    })

    wx.onBackgroundAudioPlay(function(e){
      console.log('音乐播放')
    })

    wx.onBackgroundAudioPause(function(e){
      console.log('音乐暂停')
    })

    wx.onBackgroundAudioStop(function(e){
      console.log('音乐停止')
    })
  },
  // 设置播放15秒
  setBackgroundAudio:function(){
    wx.seekBackgroundAudio({
      position: 15,
      success:function(e){
        console.log('设置成功',e)
      },
      fail:function(e){
        console.log('设置失败',e)
      }
    })
  },
  // 停止后台播放
  stopBackgroundAudio:function(){
    wx.stopBackgroundAudio()
  },
  //开始播放
  audioManagerPlay:function(){
    backgroundAudioManager.src = 'http://ws.stream.qqmusic.qq.com/M500001VfvsJ21xFqb.mp3?guid=ffffffff82def4af4b12b3cd9337d5e7&uin=346897220&vkey=6292F51E1E384E061FF02C31F716658E5C81F5594D561F2E88B854E81CAAB7806D5E4F103E55D33C16F3FAC506D1AB172DE8600B37E43FAD&fromtag=46'
    backgroundAudioManager.play()
  },
  //暂停播放
  audioManagerPause:function(){
    backgroundAudioManager.pause()
  },
  // 设置播放时间
  audioManagerSeek:function(){
    backgroundAudioManager.seek(30)
  },
  // 播放器播放
  audioPlay:function(){
    this.audioCtx.play()
  },
  // 播放器停止
  audioPause:function(){
    this.audioCtx.pause()
  },
  // 播放器设置15秒
  audioSeek:function(){
    this.audioCtx.seek(15)
  },
  // 视频录制
  vedioRecord:function(){
    var that = this
    wx.chooseVideo({
      sourceType: ['camera'],
      maxDuration:30,
      camera:'back',
      success:function(res){
        console.log(res)
        that.setData({
          vidioSrc:res.tempFilePath
        })
      },
      fail:function(res){
        console.log('录制失败')
      }
    })
  },
  // 保存录制的视频
  saveVedio:function(){
    wx.saveVideoToPhotosAlbum({
      filePath: this.data.vidioSrc,
      success:function(e){
        console.log('保存成功')
      },
      fail:function(e){
        console.log('保存失败')
      }
    })
  },
  // 停止
  pasueVideo:function(){
    this.videoCtx.pause()
  },
  // 播放
  playVideo:function(){
    this.videoCtx.play()
  },
  // 发送弹幕
  sendDanmu:function(){
    this.videoCtx.sendDanmu({
      text:this.data.Danmu,
      color: getRandomColor()
    })
    this.setData({
      Danmu:''
    })
  },
  // 弹幕内容
  bindDanmu:function(e){
    this.setData({
      Danmu:e.detail.value
    })
  },
  // 全屏
  fullScreen:function(){
    this.videoCtx.requestFullScreen()
  },
  // 拍照
  takePhoto:function(){
    var cameraCtx = wx.createCameraContext()
    cameraCtx.takePhoto({
      quality:'high',
      success:function(res){
        console.log(res.tempImagePath)
      },
      fail:function(res){
        console.log('拍照失败')
      }
    })
  },
  // 开始录像
  startRecord:function(){
    var cameraCtx = wx.createCameraContext()
    cameraCtx.startRecord({
      success:function(e){
        console.log('开始录像')
      },
      fail:function(e){
        console.log('录像失败')
      },
      timeoutCallback:function(res){
        console.log('录像结束',res)
      }
    })
  },
  // 停止录像
  stopRecord:function(){
    var cameraCtx = wx.createCameraContext()
    var that = this
    cameraCtx.stopRecord({
      success:function(e){
        console.log('录像手动结束', e)
        that.setData({
          vidioSrc:e.tempVideoPath
        })
      },
      fail:function(e){
        console.log('停止录像失败')
      }
    })
  }
})